import { IFeidaoAiBrowserComponent } from '@dfeidao/atom-web/interfaces';
import get from '@dfeidao/atom-web/local/get';
import nodejs from '@dfeidao/atom-web/msg/nodejs';
import error from '@dfeidao/atom-web/msgbox/error';
import refresh from '@dfeidao/atom-web/url/refresh';
import { productid } from '../../atom/config';

export default async function a004(fd: IFeidaoAiBrowserComponent) {
	// todo
	// 题库类型
	const question_bank_type=get(fd,'newArr');
	// 题目类型
	const leixing1=fd.data.node.querySelector<HTMLSelectElement>('[data-id="leixing"]');
	const topic_type=leixing1.value;




	// 题目内容
	const topic1=fd.data.node.querySelector<HTMLSelectElement>('[data-id="topic"]');
	const topic=topic1.value;
	const re = await nodejs<{dt: Array<{topic: string}>}>('question-bank/zj-000004/s002',{
		filter:{
		question_bank_type,
			topic_type
		}
	});
	for (let i=0;i < re.dt.length;i++) {
		if (topic === re.dt[i].topic) {
			error("这道题已存在");
			return;
		}
		}
	// 选项Adata-id="option_a"
	const option_a1=fd.data.node.querySelector<HTMLSelectElement>('[data-id="option_a"]');
	const option_a=option_a1.value;
	// 选项B
	const option_b1=fd.data.node.querySelector<HTMLSelectElement>('[data-id="option_b"]');
	const option_b=option_b1.value;
	// 选项C
	const option_c1=fd.data.node.querySelector<HTMLSelectElement>('[data-id="option_c"]');
	const option_c=option_c1.value;
	// 选项D
	const option_d1=fd.data.node.querySelector<HTMLSelectElement>('[data-id="option_d"]');
	const option_d=option_d1.value;
	// 选项
	const xuanxiangB=fd.data.node.querySelectorAll<HTMLInputElement>('[data-id="xuanxiangB"]:checked');
	const xuanxiangC=fd.data.node.querySelectorAll<HTMLInputElement>('[data-id="xuanxiangC"]:checked');
	const xuanxiangD=fd.data.node.querySelectorAll<HTMLInputElement>('[data-id="xuanxiangD"]:checked');
	const xuanxiangA=fd.data.node.querySelectorAll<HTMLInputElement>('[data-id="xuanxiangA"]:checked');
	if (topic === "" || option_a === "" || option_b === "" || option_c === "" || option_d === "") {
		error("请完善题目信息");
		return;
	} else {
		let right_answer="";
		if (topic_type === "多选题") {
			if (xuanxiangA.length > 0 && xuanxiangC.length > 0 && xuanxiangB.length === 0 && xuanxiangD.length === 0) {
				right_answer = "AC";
			} else if (xuanxiangA.length > 0 && xuanxiangB.length > 0 && xuanxiangC.length === 0 && xuanxiangD.length === 0) {
				right_answer = "AB";
			} else if (xuanxiangA.length > 0 && xuanxiangB.length > 0 && xuanxiangC.length > 0 && xuanxiangD.length === 0) {
				right_answer = "ABC";
			} else if (xuanxiangA.length > 0 && xuanxiangD.length > 0 && xuanxiangC.length === 0 && xuanxiangB.length === 0) {
				right_answer = "AD";
			} else if (xuanxiangB.length > 0 && xuanxiangC.length > 0 && xuanxiangA.length === 0 && xuanxiangD.length === 0) {
				right_answer = "BC";
			} else if (xuanxiangB.length > 0 && xuanxiangD.length > 0 && xuanxiangC.length === 0 && xuanxiangA.length === 0) {
				right_answer = "BD";
			} else if (xuanxiangB.length > 0 && xuanxiangD.length > 0 && xuanxiangC.length > 0 && xuanxiangA.length === 0) {
				right_answer = "BCD";
			} else if (xuanxiangA.length > 0 && xuanxiangB.length > 0 && xuanxiangD.length > 0 && xuanxiangC.length === 0) {
				right_answer = "ABD";
			} else if (xuanxiangA.length > 0 && xuanxiangC.length > 0 && xuanxiangD.length > 0 && xuanxiangB.length === 0) {
				right_answer = "ACD";
			} else if (xuanxiangA.length > 0 && xuanxiangB.length === 0 && xuanxiangC.length === 0 && xuanxiangD.length === 0) {
				error("最低选择两项");
				return;
			} else if (xuanxiangA.length === 0 && xuanxiangB.length > 0 && xuanxiangC.length === 0 && xuanxiangD.length === 0) {
				error("最低选择两项");
				return;

			} else if (xuanxiangA.length === 0 && xuanxiangB.length === 0 && xuanxiangC.length > 0 && xuanxiangD.length === 0) {
				error("最低选择两项");
				return;

			} else if (xuanxiangA.length === 0 && xuanxiangB.length === 0 && xuanxiangC.length === 0 && xuanxiangD.length > 0) {
				error("最低选择两项");
				return;

			} else if (xuanxiangA.length === 0 && xuanxiangB.length === 0 && xuanxiangC.length === 0 && xuanxiangD.length === 0) {
				error("最低选择两项");
				return;

			} else {
				right_answer = "ABCD";


			}
		} else {
if (xuanxiangA.length > 0 && xuanxiangB.length === 0 && xuanxiangC.length === 0 && xuanxiangD.length === 0) {
				right_answer = "A";
			} else if (xuanxiangA.length === 0 && xuanxiangB.length > 0 && xuanxiangC.length === 0 && xuanxiangD.length === 0) {
				right_answer = "B";
			} else if (xuanxiangA.length === 0 && xuanxiangB.length === 0 && xuanxiangC.length > 0 && xuanxiangD.length === 0) {
				right_answer = "C";
			} else if (xuanxiangA.length === 0 && xuanxiangB.length === 0 && xuanxiangC.length === 0 && xuanxiangD.length > 0) {
				right_answer = "D";
			} else {
				error("请选择一个选项");
				return;
			}

		}
		const res = await nodejs<{code: 1|0;result: string}>('question-bank/zj-000003/s002',{
			filter:{
				productid,
				topic_type,
				topic,
				option_a,
				option_b,
				option_c,
				option_d,
				right_answer,
				question_bank_type
			}

		});
		if (res.code === 1) {
			alert("添加成功");
			refresh();

			} else {
				alert("添加失败");
			}
	}


}

